<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="报表时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="报表时间"></el-input>
    </el-form-item>
    <el-form-item label="报表人数" prop="totalreportnum">
      <el-input v-model="dataForm.totalreportnum" placeholder="报表人数"></el-input>
    </el-form-item>
    <el-form-item label="在湖北的人数" prop="hbnum">
      <el-input v-model="dataForm.hbnum" placeholder="在湖北的人数"></el-input>
    </el-form-item>
    <el-form-item label="访问湖北的人数" prop="visithbnum">
      <el-input v-model="dataForm.visithbnum" placeholder="访问湖北的人数"></el-input>
    </el-form-item>
    <el-form-item label="接触过湖北的人数" prop="touchhbnum">
      <el-input v-model="dataForm.touchhbnum" placeholder="接触过湖北的人数"></el-input>
    </el-form-item>
    <el-form-item label="接触病例的人数" prop="touchsicknessnum">
      <el-input v-model="dataForm.touchsicknessnum" placeholder="接触病例的人数"></el-input>
    </el-form-item>
    <el-form-item label="有特殊症状人数" prop="statusnum">
      <el-input v-model="dataForm.statusnum" placeholder="有特殊症状人数"></el-input>
    </el-form-item>
    <el-form-item label="看医生的人数" prop="watchdoctornum">
      <el-input v-model="dataForm.watchdoctornum" placeholder="看医生的人数"></el-input>
    </el-form-item>
    <el-form-item label="感染病毒的人数" prop="resultnum">
      <el-input v-model="dataForm.resultnum" placeholder="感染病毒的人数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          time: '',
          totalreportnum: '',
          hbnum: '',
          visithbnum: '',
          touchhbnum: '',
          touchsicknessnum: '',
          statusnum: '',
          watchdoctornum: '',
          resultnum: ''
        },
        dataRule: {
          time: [
            { required: true, message: '报表时间不能为空', trigger: 'blur' }
          ],
          totalreportnum: [
            { required: true, message: '报表人数不能为空', trigger: 'blur' }
          ],
          hbnum: [
            { required: true, message: '在湖北的人数不能为空', trigger: 'blur' }
          ],
          visithbnum: [
            { required: true, message: '访问湖北的人数不能为空', trigger: 'blur' }
          ],
          touchhbnum: [
            { required: true, message: '接触过湖北的人数不能为空', trigger: 'blur' }
          ],
          touchsicknessnum: [
            { required: true, message: '接触病例的人数不能为空', trigger: 'blur' }
          ],
          statusnum: [
            { required: true, message: '有特殊症状人数不能为空', trigger: 'blur' }
          ],
          watchdoctornum: [
            { required: true, message: '看医生的人数不能为空', trigger: 'blur' }
          ],
          resultnum: [
            { required: true, message: '感染病毒的人数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/report/journal/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.time = data.journal.time
                this.dataForm.totalreportnum = data.journal.totalreportnum
                this.dataForm.hbnum = data.journal.hbnum
                this.dataForm.visithbnum = data.journal.visithbnum
                this.dataForm.touchhbnum = data.journal.touchhbnum
                this.dataForm.touchsicknessnum = data.journal.touchsicknessnum
                this.dataForm.statusnum = data.journal.statusnum
                this.dataForm.watchdoctornum = data.journal.watchdoctornum
                this.dataForm.resultnum = data.journal.resultnum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/report/journal/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'time': this.dataForm.time,
                'totalreportnum': this.dataForm.totalreportnum,
                'hbnum': this.dataForm.hbnum,
                'visithbnum': this.dataForm.visithbnum,
                'touchhbnum': this.dataForm.touchhbnum,
                'touchsicknessnum': this.dataForm.touchsicknessnum,
                'statusnum': this.dataForm.statusnum,
                'watchdoctornum': this.dataForm.watchdoctornum,
                'resultnum': this.dataForm.resultnum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
